package org.one.stone.soup.grfxML.plugin.geometry;


import org.one.stone.soup.grfxML.DataLine;
import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataRectangle;
import org.one.stone.soup.grfxML.DataShape;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class Grid extends SimplePlugin {

	public static final int ARG_COLOR = 0;
	public static final int ARG_TARGET = 1;
	public static final int ARG_WIDTH = 2;
	public static final int ARG_HEIGHT = 3;
	public static final int ARG_CELL_WIDTH = 4;
	public static final int ARG_CELL_HEIGHT = 5;
	public static final int ARG_BORDER = 6;

	org.one.stone.soup.grfxML.DataColor color = new org.one.stone.soup.grfxML.DataColor();
	org.one.stone.soup.grfxML.DataNode target;;
	org.one.stone.soup.grfxML.DataSize width = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize height = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize cellWidth = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize cellHeight = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataState border = new org.one.stone.soup.grfxML.DataState();

/**
 * Mover constructor comment.
 */
public Grid (GrfxMLEngine engine){
	super(engine);

	target = new org.one.stone.soup.grfxML.DataNode(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
 getEngine().getAPI().setAutoCommit(false);

 try{
	DataShape shape = target.getShape();

	DataLine line;

	DataPoint start;
	DataPoint end;

	for(int loop=0;loop<width.getValue();loop+=cellWidth.getValue())
	{
		line = new DataLine();
		line.getColor().setValueX( color.getValueX(),this );
		line.getColor().setValueY( color.getValueY(),this );
		line.getColor().setValueZ( color.getValueZ(),this );

		start = line.getStart();
		start.setValueX(loop,this);
		start.setValueY(0,this);

		end = line.getEnd();
		end.setValueX(loop,this);
		end.setValueY(height.getValue(),this);

		shape.add( line,this );
	}

	for(int loop=0;loop<height.getValue();loop+=cellHeight.getValue())
	{
		line = new DataLine();
		line.getColor().setValueX( color.getValueX(),this );
		line.getColor().setValueY( color.getValueY(),this );
		line.getColor().setValueZ( color.getValueZ(),this );

		start = line.getStart();
		start.setValueX(0,this);
		start.setValueY(loop,this);

		end = line.getEnd();
		end.setValueX(width.getValue(),this);
		end.setValueY(loop,this);

		shape.add( line,this );
	}

	if(border.getValue()==true)
	{
		DataRectangle rect = new DataRectangle();
		rect.getColor().setValueX( color.getValueX(),this );
		rect.getColor().setValueY( color.getValueY(),this );
		rect.getColor().setValueZ( color.getValueZ(),this );

		end = rect.getEnd();
		end.setValueX( width.getValue(),this );
		end.setValueY( height.getValue(),this );
		rect.getFilled().setValue( false,this );

		shape.add( rect,this );
	}
 }
 catch(LockException le){}

 getEngine().getAPI().commit();
}
public void process()
{
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{
	color = grfxMLCaster.cast(color,getArg(color,ARG_COLOR,store));
	target = grfxMLCaster.cast(target,getArg(target,ARG_TARGET,store));
	width = grfxMLCaster.cast(width,getArg(width,ARG_WIDTH,store));
	height = grfxMLCaster.cast(height,getArg(height,ARG_HEIGHT,store));
	cellWidth = grfxMLCaster.cast(cellWidth,getArg(cellWidth,ARG_CELL_WIDTH,store));
	cellHeight = grfxMLCaster.cast(cellHeight,getArg(cellHeight,ARG_CELL_HEIGHT,store));
	border = grfxMLCaster.cast(border,getArg(border,ARG_BORDER,store));
}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{
	color = grfxMLCaster.replace(color,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	width = grfxMLCaster.replace(width,oldObj,newObj);
	height = grfxMLCaster.replace(height,oldObj,newObj);
	cellWidth = grfxMLCaster.replace(cellWidth,oldObj,newObj);
	cellHeight = grfxMLCaster.replace(cellHeight,oldObj,newObj);
	border = grfxMLCaster.replace(border,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
